package RLK_JNDI;

public class Example_01 {
    public NamingEnumeration<SearchResult> search(final String providerURL,
                                                  final String name,
                                                  final String filter,
                                                  final SearchControls cons) {
        try {
            Hashtable environment = new Hashtable();
            environment.put(INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            environment.put(Context.PROVIDER_URL, providerURL);
            environment.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);
            InitialLdapContext context = new InitialLdapContext(environment, null);

            NamingEnumeration<SearchResult> result = context.search(name, filter, cons);  // Resource allocated
            return result;
        } catch (NamingException e) {
            System.err.println("LDAP search failed: " + e.getMessage());
        }
        return null;
    }
}
